home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / Open Me for REALbasic 3 / REALbasic 3.2 / Example Projects / Techniques / Examples by Thomas Tempelmann / TT's FileMgr-Plugin / Source Code (CW Pro 3) / Plugin SDK Includes / PluginMain.cpp next >
C/C++ Source or Header  |  1999-02-09  |  11KB  |  306 lines

  1. #include "REALplugin.h"
  2. #ifndef powerc
  3. #include <A4Stuff.h>
  4. #include <SetupA4.h>
  5. #endif
  6. #include "rb_plugin.h"
  7.  
  8.  
  9. static void *(*gResolver)(const char *entryName);
  10.  
  11. #ifdef powerc
  12. static void *(*gResolverPPC)(const char *entryName);
  13. #endif
  14.  
  15. static Ptr *gA4Stack;
  16.  
  17. #ifdef powerc
  18. int __procinfo = kCStackBased | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4));
  19. #endif
  20.  
  21. #ifdef powerc
  22. //#define CallResolver(a) CallUniversalProc((RoutineDescriptor *) gResolver, kCStackBased | RESULT_SIZE(SIZE_CODE(4)) | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4)), a)
  23. //#define Caller(func, upp) CallUniversalProc((RoutineDescriptor *) func, upp, 
  24. #define CallResolver(a) gResolverPPC(a)
  25. #define Caller(func, upp) func(
  26. #else
  27. #define CallResolver(a) gResolver(a)
  28. #define Caller(func, upp) func(
  29. #endif
  30.  
  31. /*
  32. void GraphicsDrawLine(Ptr graphicsObject, int x1, int y1, int x2, int y2)
  33. {
  34.     static void (*pDrawLine)(Ptr graphicsObject, int x1, int y1, int x2, int y2);
  35.     if (!pDrawLine)
  36.         pDrawLine = (void (*)(Ptr graphicsObject, int x1, int y1, int x2, int y2)) CallResolver("RuntimeGraphicsDrawLine");
  37.     pDrawLine(graphicsObject, x1, y1, x2, y2);
  38. }
  39. */
  40. /*
  41. void REALRegisterClass(REALobjectDefinition *defn)
  42. {
  43.     static void (*pRuntimeRegisterClass)(REALobjectDefinition *defn);
  44.     if (!pRuntimeRegisterClass)
  45.         pRuntimeRegisterClass = (void (*)(REALobjectDefinition *)) CallResolver("PluginRegisterClass");
  46.     Caller(pRuntimeRegisterClass, kCStackBased | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4))) defn);
  47. }
  48. */
  49. void REALRegisterMethod(REALmethodDefinition *defn)
  50. {
  51.     static void (*pRuntimeRegisterMethod)(REALmethodDefinition *defn);
  52.     if (!pRuntimeRegisterMethod)
  53.         pRuntimeRegisterMethod = (void (*)(REALmethodDefinition *)) CallResolver("PluginRegisterMethod");
  54.     Caller(pRuntimeRegisterMethod, kCStackBased | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4))) defn);
  55. }
  56.  
  57. void REALRegisterControl(REALcontrol *defn)
  58. {
  59.     static void (*pRuntimeRegisterControl)(REALcontrol *defn);
  60.     if (!pRuntimeRegisterControl)
  61.         pRuntimeRegisterControl = (void (*)(REALcontrol *)) CallResolver("PluginRegisterControl");
  62.     Caller(pRuntimeRegisterControl, kCStackBased | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4))) defn);
  63. }
  64.  
  65. const char *REALCString(REALstring str)
  66. {
  67.     static const char *(*pStringGetCString)(REALstring str);
  68.     if (!pStringGetCString)
  69.         pStringGetCString = (const char *(*)(REALstring)) CallResolver("StringGetCString");
  70.     return (const char *) Caller(pStringGetCString, kCStackBased | RESULT_SIZE(SIZE_CODE(4)) | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4))) str);
  71. }
  72.  
  73. const unsigned char *REALPString(REALstring str)
  74. {
  75.     static const unsigned char *(*pStringGetPString)(REALstring str);
  76.     if (!pStringGetPString)
  77.         pStringGetPString = (const unsigned char *(*)(REALstring)) CallResolver("StringGetPString");
  78.     return (const unsigned char *) Caller(pStringGetPString, kCStackBased | RESULT_SIZE(SIZE_CODE(4)) | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4))) str);
  79. }
  80.  
  81. REALstring REALBuildString(const char *contents, int length)
  82. {
  83.     static REALstring (*pREALBuildString)(const char *contents, int length);
  84.     if (!pREALBuildString)
  85.         pREALBuildString = (REALstring (*)(const char *, int)) CallResolver("REALBuildString");
  86.     return pREALBuildString(contents, length);
  87. }
  88.  
  89. REALpicture REALBuildPictureFromPicHandle(PicHandle pic, Boolean bPassOwnership)
  90. {
  91.     static REALpicture (*pREALBuildPictureFromPicHandle)(PicHandle, Boolean);
  92.     if (!pREALBuildPictureFromPicHandle)
  93.         pREALBuildPictureFromPicHandle = (REALpicture (*)(PicHandle, Boolean)) CallResolver("REALBuildPictureFromPicHandle");
  94.     return pREALBuildPictureFromPicHandle(pic, bPassOwnership);
  95. }
  96.  
  97. REALpicture REALBuildPictureFromGWorld(GWorldPtr world, Boolean bPassOwnership)
  98. {
  99.     static REALpicture (*pREALBuildPictureFromGWorld)(GWorldPtr, Boolean);
  100.     if (!pREALBuildPictureFromGWorld)
  101.         pREALBuildPictureFromGWorld = (REALpicture (*)(GWorldPtr, Boolean)) CallResolver("REALBuildPictureFromGWorld");
  102.     return pREALBuildPictureFromGWorld(world, bPassOwnership);
  103. }
  104.  
  105. void *REALGetEventInstance(REALcontrolInstance instance, REALevent *event)
  106. {
  107.     static void *(*pGetEventInstance)(REALcontrolInstance instance, int builtHook);
  108. #ifdef powerc
  109.     if (!pGetEventInstance)
  110.         pGetEventInstance = (void *(*)(REALcontrolInstance,int)) CallResolver("GetEventInstancePPC");
  111.     return (void *) Caller(pGetEventInstance, kCStackBased | RESULT_SIZE(SIZE_CODE(4)) | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4)) | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(4))) instance, event->forSystemUse);
  112. #else
  113.     if (!pGetEventInstance)
  114.         pGetEventInstance = (void *(*)(REALcontrolInstance,int)) CallResolver("GetEventInstance");
  115.     return (void *) Caller(pGetEventInstance, kCStackBased | RESULT_SIZE(SIZE_CODE(4)) | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4)) | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(4))) instance, event->forSystemUse);
  116. #endif
  117. }
  118.  
  119. void *REALGetControlData(REALcontrolInstance instance, REALcontrol *defn)
  120. {
  121.     return ((Ptr) instance) + defn->forSystemUse;
  122. }
  123.  
  124. void REALGetControlBounds(REALcontrolInstance instance, Rect *rBounds)
  125. {
  126.     static void (*pGetControlBounds)(REALcontrolInstance, Rect *);
  127.     if (!pGetControlBounds)
  128.         pGetControlBounds = (void (*)(REALcontrolInstance, Rect *)) CallResolver("GetControlBounds");
  129.     Caller(pGetControlBounds, kCStackBased | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4)) | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(4))) instance, rBounds);
  130. }
  131.  
  132. REALfolderItem REALFolderItemFromFSSpec(const FSSpec *spec)
  133. {
  134.     static REALfolderItem (*pFolderItemFromFSSpec)(const FSSpec *);
  135.     if (!pFolderItemFromFSSpec)
  136.         pFolderItemFromFSSpec = (REALfolderItem (*)(const FSSpec *)) CallResolver("FolderItemFromFSSpec");
  137.     return pFolderItemFromFSSpec(spec);
  138. }
  139.  
  140. Boolean REALFSSpecFromFolderItem(FSSpec *spec, REALfolderItem item)
  141. {
  142.     static Boolean (*pREALFSSpecFromFolderItem)(FSSpec *, REALfolderItem);
  143.     if (!pREALFSSpecFromFolderItem)
  144.         pREALFSSpecFromFolderItem = (Boolean (*)(FSSpec *, REALfolderItem)) CallResolver("REALFSSpecFromFolderItem");
  145.     return pREALFSSpecFromFolderItem(spec, item);
  146. }
  147.  
  148. void REALSelectGraphics(REALgraphics context)
  149. {
  150.     static void (*pSelectGraphics)(REALgraphics);
  151.     if (!pSelectGraphics)
  152.         pSelectGraphics = (void (*)(REALgraphics)) CallResolver("SelectGraphics");
  153.     pSelectGraphics(context);
  154. }
  155.  
  156. REALsound REALBuildSoundFromHandle(Handle sound, Boolean bPassOwnership)
  157. {
  158.     static REALsound (*pREALBuildSoundFromHandle)(Handle, Boolean);
  159.     if (!pREALBuildSoundFromHandle)
  160.         pREALBuildSoundFromHandle = (REALsound (*)(Handle, Boolean)) CallResolver("REALBuildSoundFromHandle");
  161.     return pREALBuildSoundFromHandle(sound, bPassOwnership);
  162. }
  163.  
  164. REALappleEvent REALBuildAppleEvent(const AppleEvent *event, Boolean bPassOwnership)
  165. {
  166.     static REALappleEvent (*pREALBuildAppleEvent)(const AppleEvent *);
  167.     if (!pREALBuildAppleEvent)
  168.         pREALBuildAppleEvent = (REALappleEvent (*)(const AppleEvent *)) CallResolver("REALBuildAppleEvent");
  169.     return pREALBuildAppleEvent(event);
  170. }
  171.  
  172. REALappleEvent REALBuildAEDescList(const AppleEvent *event, Boolean bPassOwnership)
  173. {
  174.     static REALappleEvent (*pREALBuildAEDescList)(const AppleEvent *);
  175.     if (!pREALBuildAEDescList)
  176.         pREALBuildAEDescList = (REALappleEvent (*)(const AppleEvent *)) CallResolver("REALBuildAEDescList");
  177.     return pREALBuildAEDescList(event);
  178. }
  179.  
  180. REALappleEvent REALBuildAEObjSpecifier(const AppleEvent *event, Boolean bPassOwnership)
  181. {
  182.     static REALappleEvent (*pREALBuildAEObjSpecifier)(const AppleEvent *);
  183.     if (!pREALBuildAEObjSpecifier)
  184.         pREALBuildAEObjSpecifier = (REALappleEvent (*)(const AppleEvent *)) CallResolver("REALBuildAEObjSpecifier");
  185.     return pREALBuildAEObjSpecifier(event);
  186. }
  187.  
  188. AppleEvent *REALAccessAppleEvent(REALappleEvent event)
  189. {
  190.     static AppleEvent *(*pREALAccessAppleEvent)(REALappleEvent);
  191.     if (!pREALAccessAppleEvent)
  192.         pREALAccessAppleEvent = (AppleEvent *(*)(REALappleEvent)) CallResolver("REALAccessAppleEvent");
  193.     return pREALAccessAppleEvent(event);
  194. }
  195.  
  196. AppleEvent *REALAccessAppleEventReply(REALappleEvent event)
  197. {
  198.     static AppleEvent *(*pREALAccessAppleEventReply)(REALappleEvent);
  199.     if (!pREALAccessAppleEventReply)
  200.         pREALAccessAppleEventReply = (AppleEvent *(*)(REALappleEvent)) CallResolver("REALAccessAppleEventReply");
  201.     return pREALAccessAppleEventReply(event);
  202. }
  203.  
  204. WindowPtr REALGetWindowHandle(REALwindow window)
  205. {
  206.     static WindowPtr (*pREALGetWindowHandle)(REALwindow);
  207.     if (!pREALGetWindowHandle)
  208.         pREALGetWindowHandle = (WindowPtr (*)(REALwindow)) CallResolver("REALGetWindowHandle");
  209.     return pREALGetWindowHandle(window);
  210. }
  211.  
  212. ControlHandle REALGetControlHandle(REALcontrolInstance control)
  213. {
  214.     static ControlHandle (*pREALGetControlHandle)(REALcontrolInstance);
  215.     if (!pREALGetControlHandle)
  216.         pREALGetControlHandle = (ControlHandle (*)(REALcontrolInstance)) CallResolver("REALGetControlHandle");
  217.     return pREALGetControlHandle(control);
  218. }
  219.  
  220. MenuHandle REALGetPopupMenuHandle(REALpopupMenu popup)
  221. {
  222.     static MenuHandle (*pREALGetPopupMenuHandle)(REALpopupMenu);
  223.     if (!pREALGetPopupMenuHandle)
  224.         pREALGetPopupMenuHandle = (MenuHandle (*)(REALpopupMenu)) CallResolver("REALGetPopupMenuHandle");
  225.     return pREALGetPopupMenuHandle(popup);
  226. }
  227.  
  228. void main(void *(*resolver)(const char *entryName))
  229. {
  230.     short *glue;
  231.     int i;
  232.     void (*pRegisterPluginExports)(REALexport *table);
  233.  
  234. #ifndef powerc
  235.     EnterCodeResource();
  236. #endif
  237.     gResolver = resolver;
  238.  
  239. #ifndef powerc
  240.     unsigned long unglue;
  241.     unsigned long a4stack;
  242.     unsigned long (*getA4stack)(void);
  243.     long pluginA4;
  244.     unsigned long originalCode;
  245.     void *glueProc;
  246.  
  247.     getA4stack = (unsigned long(*)(void)) resolver("getA4stackReference");
  248.  
  249.     glue = (short *) NewPtr(pluginExportCode * 52);
  250.     a4stack = getA4stack();
  251.     pluginA4 = GetCurrentA4();
  252.  
  253.     for (i = 0; i < pluginExportCode; i++)
  254.     {
  255.         unglue = (unsigned long) (glue + 17);
  256.         originalCode = (unsigned long) pluginExports[i].proc;
  257.  
  258.         glueProc = glue;
  259.  
  260.         *glue++ = 0x2079; // movea.l $, A0
  261.         *glue++ = (short) (a4stack >> 16);
  262.         *glue++ = (short) (a4stack & 0xffff);
  263.         *glue++ = 0x210c; // move.l A4, -(A0)
  264.         *glue++ = 0x211f; // move.l (A7)+, -(A0)
  265.         *glue++ = 0x287c; // move.l #, A4
  266.         *glue++ = (short) (pluginA4 >> 16);
  267.         *glue++ = (short) (pluginA4 & 0xffff);
  268.         *glue++ = 0x2f3c; // move.l #, -(A7)
  269.         *glue++ = (short) (unglue >> 16);
  270.         *glue++ = (short) (unglue & 0xffff);
  271.         *glue++ = 0x23c8; // move.l A0, $
  272.         *glue++ = (short) (a4stack >> 16);
  273.         *glue++ = (short) (a4stack & 0xffff);
  274.         *glue++ = 0x4ef9; // jmp $
  275.         *glue++ = (short) (originalCode >> 16);
  276.         *glue++ = (short) (originalCode & 0xffff);
  277.  
  278.         *glue++ = 0x2279; // movea.l $, A1
  279.         *glue++ = (short) (a4stack >> 16);
  280.         *glue++ = (short) (a4stack & 0xffff);
  281.         *glue++ = 0x2f19; // move.l (A1)+,-(A7)
  282.         *glue++ = 0x2859; // movea.l (A1)+, A4
  283.         *glue++ = 0x23c9; // move.l A1, $
  284.         *glue++ = (short) (a4stack >> 16);
  285.         *glue++ = (short) (a4stack & 0xffff);
  286.         *glue++ = 0x4e75; // rts
  287.  
  288.         pluginExports[i].proc = glueProc;
  289.     }
  290. #else
  291.     gResolverPPC = (void *(*)(const char *)) CallUniversalProc((RoutineDescriptor *) gResolver, kCStackBased | RESULT_SIZE(SIZE_CODE(4)) | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4)), "ResolverPPC");
  292.  
  293.     for (i = 0; i < pluginExportCode; i++)
  294.         pluginExports[i].flags |= REALexportPPC;
  295. #endif
  296.  
  297.     pRegisterPluginExports = (void (*)(REALexport *)) CallResolver("RegisterPluginExports");
  298.     Caller(pRegisterPluginExports, kCStackBased | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(4))) pluginExports);
  299.  
  300.     PluginEntry();
  301.  
  302. #ifndef powerc
  303.     ExitCodeResource();
  304. #endif
  305. }
  306.